package com.csw.empGrey2.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.csw.empGrey2.dao.EmpDao;
import com.csw.empGrey2.entity.Emp;

/**
 * @author 1
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class EmpServiceImpl implements EmpService {
	@Autowired
	private EmpDao empDao;

	@Transactional(rollbackFor = Exception.class)
	@Override
	public Emp findByCardId(Integer cardId) {
		return empDao.findByCardId(cardId);
	}

	@Override
	public void deleteAccount(Integer cardId) {
		empDao.deleteAccount(cardId);
	}

	@Override
	public void updateBy(Emp emp) {
		empDao.updateBy(emp);
	}

	@Override
	public void addEmp(Emp emp) {
		empDao.addEmp(emp);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public Integer countPage(Integer pageSize) {
		Integer countPage = 0;
		Integer count = empDao.queryCount();
		if (count % pageSize == 0) {
			countPage = count / pageSize;
		} else {
			countPage = count / pageSize + 1;
		}
		return countPage;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public List<Emp> queryAllAndByPage(Integer currPage, Integer pageSize, Integer countPage) {
		List<Emp> deptList = null;
		Integer firstIndex;
		Integer lastIndex;

		if (!currPage.equals(countPage)) {
			firstIndex = (currPage - 1) * pageSize + 1;
			lastIndex = currPage * pageSize;
		} else {
			firstIndex = (currPage - 1) * pageSize + 1;
			lastIndex = empDao.queryCount();
		}
		return empDao.queryAllAndByPage(firstIndex, lastIndex);
	}
}